//
//  ModifyPasswordViewCell.m
//  myapp
//
//  Created by 安建伟 on 2019/7/31.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "ModifyPasswordViewCell.h"

@implementation ModifyPasswordViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self) {
        if (!_passwordTF) {
            _passwordTF = [[UITextField alloc] init];
            _passwordTF.backgroundColor = [UIColor clearColor];
            _passwordTF.font = [UIFont fontWithName:@"Arial" size:15.0f];
            //_passwordTF.textColor = [UIColor colorWithHexString:@"222222"];
            //_passwordTF.borderStyle = UITextBorderStyleRoundedRect;
            _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
            _passwordTF.autocorrectionType = UITextAutocorrectionTypeNo;
            _passwordTF.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            //设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
            _passwordTF.adjustsFontSizeToFitWidth = YES;
            //设置自动缩小显示的最小字体大小
            _passwordTF.minimumFontSize = 11.f;
            [_passwordTF addTarget:self action:@selector(textFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
            [self.contentView addSubview:_passwordTF];
            
            [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(yAutoFit(260.f), yAutoFit(30.f)));
                make.centerY.equalTo(self.contentView.mas_centerY);
                make.left.equalTo(self.contentView.mas_left).offset(25.f);
            }];
        }
        if (!_passwordVisiableBtn) {
            _passwordVisiableBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [_passwordVisiableBtn setImage:[UIImage imageNamed:@"img_pwd_unvisiable"] forState:UIControlStateNormal];
            [_passwordVisiableBtn addTarget:self action:@selector(passwordVisiableControl) forControlEvents:UIControlEventTouchUpInside];
            _passwordVisiableBtn.tag = yUnselect;//默认隐藏，选择后显示
            _passwordVisiableBtn.hidden = NO;
            [self.contentView addSubview:_passwordVisiableBtn];
            [_passwordVisiableBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(yAutoFit(60),yAutoFit(44.f)));
                make.right.equalTo(self.contentView.mas_right).offset(-20.f);
                make.centerY.equalTo(self.contentView.mas_centerY);
            }];
        }
        
    }
    return self;
}

- (void)passwordVisiableControl{
    if (_passwordVisiableBtn.tag == yUnselect) {
        _passwordVisiableBtn.tag = ySelect;
        [_passwordVisiableBtn setImage:[UIImage imageNamed:@"img_pwd_visiable"] forState:UIControlStateNormal];
        _passwordTF.secureTextEntry = NO;
    }else{
        _passwordVisiableBtn.tag = yUnselect;
        [_passwordVisiableBtn setImage:[UIImage imageNamed:@"img_pwd_unvisiable"] forState:UIControlStateNormal];
        _passwordTF.secureTextEntry = YES;
    }
}

-(void)textFieldTextChange:(UITextField *)textField{
    if (self.TFBlock) {
        self.TFBlock(textField.text);
    }
}

@end
